package gold.enterprise;

import java.util.Deque;
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;

public class Zhihu {


    class ConcurrentQueue {
        public Deque<Integer> queue = new LinkedList();

        public int limit = 10;

        public AtomicInteger queueSize = new AtomicInteger(0);

        public void enQueue(int val) throws Exception {
            synchronized (queueSize) {
                while (queueSize.get() > limit) {
                    this.queueSize.wait();
                }

                this.queue.offerLast(val);
                this.queueSize.incrementAndGet();//加size
                this.queue.notifyAll();
            }
        }

        public int deQueue() throws Exception {
            synchronized (queueSize) {
                if (queueSize.get() <= 0) {
                    throw new Exception("empty queue cannot be poll!");
                }

                int val = this.queue.pollFirst();
                this.queueSize.decrementAndGet();//减size
                this.queue.notifyAll();
                return val;
            }
        }
    }

    public void run() {
        ConcurrentQueue cq = new ConcurrentQueue();
        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
//                    cq.enQueue(Random);
                }
            }).start();
        }

    }

    public static char[] swapStr(char[] str) {
        if (null == str) return null;
        int i = 0, j = str.length - 1;
        char temp;
        while (i <= j) {
            temp = str[i];
            str[i] = str[j];
            str[j] = temp;
            ++i;
            --j;
        }

        return str;
    }


    public static void main(String[] args) throws Exception {
        Zhihu runner = new Zhihu();
        System.out.println(new String(swapStr("123456".toCharArray())));
    }


}
